#include "discovermusicmodule.h"
#include "songlist2.h"
#include "ui_discovermusicmodule.h"
#include <QLabel>
#include <QThread>
#include"login.h"
DiscoverMusicModule::DiscoverMusicModule(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DiscoverMusicModule)
{
    ui->setupUi(this);
    ui->stackedWidget->setFixedSize(1131,699);
    ui->stackedWidget->move(0,0);
    QLabel*label=new QLabel(this);
    label->setPixmap(QPixmap(":/new/prefix1/res/10.png"));
    songlist2*s=new songlist2(this);
    QVBoxLayout *pLayout = new QVBoxLayout(ui->scrollArea);
    pLayout->addWidget(label);
    pLayout->addWidget(s);
    ui->scrollArea->widget()->setLayout(pLayout);

    ui->exit->move(0,0);
    ui->label->setFixedHeight(50);
    ui->label->setFixedWidth(width()/4);
    ui->label->move(width()/2-ui->label->width()/2+150,0);
    ui->scrollArea_2->move(0,50);
    ui->scrollArea_2->setFixedSize(1131,649);
    //ui->scrollArea_2->setStyleSheet("QScrollArea { border: none; }");
    connect(s,&songlist2::showmusiclist,this,[&](list<QString>lis){
           ui->stackedWidget->setCurrentIndex(1);
           ui->label->setText("每 日 推 荐");
           m_pLayout = new QVBoxLayout(nullptr);
           int index=1;
           while(!lis.empty())
           {
               QString front=lis.front();
               MusicInfo *info=new MusicInfo(false,index,front,"无","无","无",nullptr);
               connect(info,&MusicInfo::addlove,this,&DiscoverMusicModule::addlovemusic);
               connect(info,&MusicInfo::dellove,this,&DiscoverMusicModule::dellovemusic);
               m_pLayout->addWidget(info);
               lis.pop_front();
               connect(info,&MusicInfo::doubleClicked,this,[&](tcpclient*client,QString title){
                    emit playmusic(client,title);
               });
               index++;
           }

           if(index<50)   //站位
           {
               for(int i=0;i<10;i++){
                    QWidget*w=new QWidget;
                    m_pLayout->addWidget(w);
                   //musiclist.push_back(info);
               }
           }
           ui->scrollArea_2->setWidgetResizable(true);
           ui->scrollArea_2->widget()->setLayout(m_pLayout);

    });
}

DiscoverMusicModule::~DiscoverMusicModule()
{
    delete ui;
}

//添加喜欢音乐
//usrnameid:{1373474316}\r\npassword:{137347}\r\nAddLoveMusic:{反方向的钟}\r\n
//删除喜欢音乐
//usrnameid:{1373474316}\r\npassword:{137347}\r\nDelLoveMusic:{反方向的钟}\r\n
void DiscoverMusicModule::addlovemusic(QString title)
{
      MusicFlag flag=MusicFlag::addmusic;
      std::thread* pa=new std::thread(threadaddmusic,m_nameid,m_passwd,title,flag,this);
      pa->detach();
}

void DiscoverMusicModule::dellovemusic(QString title)
{
    MusicFlag flag=MusicFlag::delmuisc;
    std::thread* pa=new std::thread(threadaddmusic,m_nameid,m_passwd,title,flag,this);
    pa->detach();
}

void DiscoverMusicModule::threadaddmusic(QString nameid,QString passwd,QString title,MusicFlag flag,DiscoverMusicModule*D)
{
    tcpclient*client=new tcpclient();
    QString request;
    request += QString("usrnameid:{%1}\\r\\n").arg(nameid);
    request += QString("password:{%1}\\r\\n").arg(passwd);

    if(flag==MusicFlag::addmusic)
    {
       request += QString("AddLoveMusic:{%1}\\r\\n").arg(title);
    }
    else
    {
       request += QString("DelLoveMusic:{%1}\\r\\n").arg(title);
    }
    client->write(request);
    client->send();
    while(1)
    {
        // 检查连接状态
        if (client->m_socket->state() == QAbstractSocket::UnconnectedState) {
            // 连接已关闭，跳出while循环
            break;
        }
        if(!client->m_socket->waitForReadyRead())
        {
            continue;
        }
        client->Receiving.append(client->m_socket->readAll());

    }
/*
*   Responsecode:{ok}\r\n     //OK成功  NoFound //没有找到资源
    usrnameid:{}\r\n
    AddLoveMusic:{succes}\r\n    notfound 用于密码或账号错误  OK登录成功
    \r\n
    data
*/
   QByteArray response=client->getnetworkdata();
   delete  client;
   client=nullptr;
   QVector<Body> arrbody;
   QByteArray finalData;   //最后的数据 这里没有
   parseData(response, arrbody,finalData);

   if(arrbody[2].key=="AddLoveMusic")   //添加喜欢音乐返回报文
   {
      if(arrbody[2].value=="success")   //成功
      {
         emit D->NotifyShow("AddLoveMusic","success");
      }
      else
      {
         emit D->NotifyShow("AddLoveMusic","Failing");
      }
   }
   else if(arrbody[2].key=="DelLoveMusic") //删除喜欢音乐返回报文
   {
       if(arrbody[2].value=="success")   //成功
       {
          emit D->NotifyShow("DelLoveMusic","success");
       }
       else
       {
          emit D->NotifyShow("DelLoveMusic","Failing");
       }
   }


}

void DiscoverMusicModule::on_exit_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
    QLayoutItem* item;
    while ((item = m_pLayout->takeAt(0)) != nullptr)
    {
        delete item->widget();
        delete item;
    }
    delete  m_pLayout;
    m_pLayout=nullptr;
    ui->scrollArea_2->widget()->setLayout(nullptr);
}
